package fr.charlott.view;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import fr.charlott.business.dto.HotesseDTO;
import fr.charlott.business.dto.ReunionDTO;
import fr.charlott.business.service.ReunionService;
import fr.charlott.tools.exceptions.FunctionalException;
import fr.charlott.tools.exceptions.TechnicalException;

@Component("nouvelleReunionControler")
@Scope("view")
public class NouvelleReunionControler extends CharlottControler {

    private static final long serialVersionUID = -6404906491610002771L;

    public static final String EDITED_REUNION = "editedReunion";

    /**
     * Services
     */
    private ReunionService reunionService;

    private ReunionDTO reunion;

    private List<HotesseDTO> hotesses;

    private HotesseDTO selectedHotesse;

    private String labelPage = "Modifier une réunion";

    public NouvelleReunionControler() {
        super();
    }

    @Autowired(required = true)
    public NouvelleReunionControler(ReunionService reunionService) {
        super();
        this.reunionService = reunionService;

        // Recupere la reunion selectionnee dans la session
        ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        reunion = (ReunionDTO) externalContext.getSessionMap().get(EDITED_REUNION);

        if (reunion == null) {
            reunion = new ReunionDTO();
            labelPage = "Ajouter une réunion";
        }
    }

    // actionListener
    public void selectionnerHotesseListener(ActionEvent actionEvent) {
        reunion.setHotesse(selectedHotesse);
    }

    // navigation

    public String ajouterReunion() throws TechnicalException, FunctionalException {
        try {
            reunionService.ajouterReunion(reunion);
            cleanSession(EDITED_REUNION);
            return "listeReunions";
        } catch (TechnicalException e) {
            FacesMessage msg = new FacesMessage("Error", e.getMessage());
            FacesContext.getCurrentInstance().addMessage(null, msg);
            return null;
        }
    }

    public String annuler() {
        cleanSession(EDITED_REUNION);
        return "listeReunions";
    }

    // actionListener
    public void selectionnerHotesseListener(AjaxBehaviorEvent event) {
        reunion.setHotesse(selectedHotesse);
    }

    // getters / setters

    public ReunionDTO getReunion() {
        return reunion;
    }

    public List<HotesseDTO> getHotesses() {
        return hotesses;
    }

    public HotesseDTO getSelectedHotesse() {
        if (reunion.getHotesse() != null)
            return reunion.getHotesse();
        return selectedHotesse;
    }

    public void setSelectedHotesse(HotesseDTO selectedHotesse) {
        this.selectedHotesse = selectedHotesse;
    }

    public String getLabelPage() {
        return labelPage;
    }
}